import 'package:flutter/material.dart';
import 'package:flutter_common_app/api/get_playlist_detail_api.dart';
import 'package:flutter_common_app/data/play_song_data.dart';
import 'package:flutter_common_app/data/playlist_data.dart';
import 'package:flutter_common_app/data/recommend_playlist_data.dart';
import 'package:flutter_common_app/pages/playlist_detail/playlist_detail_sliver_header.dart';
import 'package:flutter_common_app/provider_model/audio_model.dart';
import 'package:flutter_common_app/widgets/empty_view/v_empty_view.dart';
import 'package:flutter_common_app/widgets/songs_list_item.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///歌单详情
class PlaylistDetailPage extends StatefulWidget {
  final RecommendPlaylistData recommendPlaylistData;

  PlaylistDetailPage(this.recommendPlaylistData, {Key key});

  @override
  _PlaylistDetailPageState createState() => _PlaylistDetailPageState();
}

class _PlaylistDetailPageState extends State<PlaylistDetailPage> {
  PlaylistData playlistData;

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      GetPlaylistDetailApi(context)
        ..getParams()['id'] = widget.recommendPlaylistData.id
        ..send().then((res) {
          setState(() {
            playlistData = PlaylistData.fromJson(res.data['playlist']);
          });
        });
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverPersistentHeader(
            delegate: PlaylistDetailSliverHeader(
                widget.recommendPlaylistData, playlistData),
            pinned: true,
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext build, int index) {
                TracksData data = playlistData.tracks[index];
                return SongsListItem(
                  name: data.name,
                  describe:
                      '${data.ar.map((a) => a.name).toList().join('/')} - ${data.al.name}',
                  index: index,
                  clickPlayCallBack: () {
                    Provider.of<AudioModel>(context, listen: false).playOneSong(
                      PlaySongData(
                        data.id,
                        name: data.name,
                        artists: data.al.name,
                        picUrl: data.al.picUrl,
                      ),
                    );
                  },
                );
              },
              childCount: playlistData?.tracks?.length ?? 0,
            ),
          ),
          SliverToBoxAdapter(
            child: VEmptyView(100.w),
          ),
        ],
      ),
    );
  }
}
